<?php
/**
 * Created by PhpStorm.
 * User: DOVEGIFT
 * Date: 2019/6/20
 * Time: 11:10
 */
header("Access-Control-Allow-Origin:*");
header("Content-Type:application/json;charset=utf-8");
if ($_SERVER["REQUEST_METHOD"] != "POST") {
    $result = array("msg" => "对不起,不支持get请求", "status" => -1);
    print_r(json_encode($result));
    return;
}
//post请求
if (!(isset($_POST["uname"]) && isset($_POST["upwd"]))) {
    $result = array("msg" => "参数不完整", "status" => -2);
    print_r(json_encode($result));
    return;
}
$UNAME = $_REQUEST["uname"];
$UPWD= $_REQUEST["upwd"];

include "DBhelper.php";

// 1.创建数据库的链接对象
$conn=new mysqli("localhost","root","","cs1902","3306");
//2.设置链接字符集
  mysqli_query($conn,"set names utf8");
//3.准备sql语句
$sql="SELECT `u_id`,`u_name` FROM `userinfo` WHERE u_name='".$UNAME."' AND u_pwd='".$UPWD."'";

//4.执行sql语句
$dataResult=$conn->query($sql);
//5.判断结果
if ($dataResult->num_rows>=1){
    //如果登陆了,就把当前登陆者信息保存到session
    session_start();
    $userData=$dataResult->fetch_assoc();
    $_SESSION["userinfo"]=$userData;   //$userData数组对象
    $result = array("msg" => "登录成功", "status" => 1,"data"=> $dataResult->fetch_assoc());
    print_r(json_encode($result));
}else{
    $result = array("msg" => "登录失败", "status" => -4);
    print_r(json_encode($result));
}
//6.关闭数据库链接
$conn->close();
